<?php
/**
 * Created by PhpStorm.
 * User: yyq
 * Date: 2018/7/10
 * Time: 11:03
 */

namespace Common\Model;


class OrderModel extends CommonModel
{
    const PAY_TYPE_ALIPAY   = 'alipay';  //支付宝支付
    const PAY_TYPE_WX       = 'wxpay';      //微信支付
    const PAY_TYPE_NO       = 'noway';//


    //订单公共状态
    const STATUS_CANCEL         = 'cancel';        //用户取消  //取消
    const STATUS_WAIT_FOR_PAY   = 'unpay';  //待付款   //未付款
    const STATUS_PAY_SUCCESS    = 'payed';  //已付款   //待发货
    const STATUS_SEND           = 'deliver';          //已发货   //待收货
    const STATUS_WAIT_COMMENT   = 'wait_comment';      //待评价
    const STATUS_COMPLETE       = 'done';      //已完成
    const STATUS_WAIT           = 'wait';      //待退款
    const STATUS_RETURN         = 'return';      //已退款

    //订单显示状态
    const SHOW_ON   = 'yes';    //显示
    const SHOW_OFF  = 'no';     //隐藏

    /**
     * 生成订单编号
     * @return string
     */
    public function getOrderNumber()
    {
        $num = 'D'.date('Ymd') . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT);
        if($this->where(['order_sn' => $num])->find()) {
            self::getOrderNumber();
        }
        return $num;
    }

    /**
     * @param $status
     * @param $page
     * @param $per_page
     * @return mixed
     */
    public function getOrderList($mid, $status, $page=1, $per_page=10) {
        if($status != 'all') {
            $where = [
                'status'  => $status,
            ];
        }
        $where['mid']   = $mid;
        $where['shows'] = self::SHOW_ON;


        $count = $this->where($where)->count();

        $result = $this->where($where)
            ->page($page, $per_page)
            ->field('id as order_id, order_sn, status, order_price, freight')
            ->order('update_time desc')
            ->select();

        $order_product_model = new OrderProductModel();
        foreach ($result as $k => $v) {
            $product = $order_product_model
                ->where(['order_id' => $v['order_id']])
                ->field('id as order_pro_id, product_name, option_name, number, price, cover_photo, status as comment_status')
                ->select();
            foreach ($product as $key => $val) {
                $product[$key]['cover_photo'] = setUrl($val['cover_photo']);
            }
            $result[$k]['status_string'] = $this->getUserStatusString($v['status']);
            $result[$k]['product'] = $product;
        }

        $data['lists'] = $result;
        $data['total_page'] = ceil($count/$per_page);
        return $data;
    }

    /**
     * @param $status
     * @return string
     */
    public function getUserStatusString($status)
    {
        switch ($status) {
            case self::STATUS_WAIT_FOR_PAY:
                return '待付款';
                break;
            case self::STATUS_PAY_SUCCESS:
                return '待发货';
                break;
            case self::STATUS_SEND:
                return '待收货';
                break;
            case self::STATUS_WAIT_COMMENT:
                return '待评价';
                break;
            case self::STATUS_COMPLETE:
                return '已完成';
                break;
            case self::STATUS_CANCEL:
                return '已取消';
                break;
            case self::STATUS_WAIT:
                return '待退款';
                break;
            case self::STATUS_RETURN:
                return '已退款';
                break;
            default :
                return '未声明状态';
                break;
        }
    }

    public function getAdminStatusToString($status)
    {
        switch ($status) {
            case self::STATUS_CANCEL:
                return '已取消';
                break;
            case self::STATUS_WAIT_FOR_PAY:
                return '待付款';
                break;
            case self::STATUS_PAY_SUCCESS:
                return '已付款';
                break;
            case self::STATUS_SEND:
                return '已发货';
                break;
            case self::STATUS_WAIT_COMMENT:
                return '待评价';
                break;
            case self::STATUS_COMPLETE:
                return '已完成';
                break;
            case self::STATUS_WAIT:
                return '待退款';
                break;
            case self::STATUS_RETURN:
                return '已退款';
                break;
            default :
                return '未声明状态';
                break;
        }
    }

    /**
     * 支付方式
     *
     * @param $type
     *
     * @return string
     */
    public function payTypetoString($type)
    {
        switch ($type) {
            case self::PAY_TYPE_ALIPAY:
                return '支付宝';
                break;
            case self::PAY_TYPE_WX:
                return '微信';
                break;
            default :
                return '-';
                break;
        }
    }

    public function getStatusOption($status='') {
        $data[] = self::STATUS_CANCEL;
        $data[] = self::STATUS_WAIT_FOR_PAY;
        $data[] = self::STATUS_PAY_SUCCESS;
        $data[] = self::STATUS_SEND;
        $data[] = self::STATUS_WAIT_COMMENT;
        $data[] = self::STATUS_COMPLETE;
        $data[] = self::STATUS_WAIT;
        $data[] = self::STATUS_RETURN;


        $options = '';
        foreach($data as $k => $v) {
            if($status == $v) {
                $options .= '<option selected value="' . $v . '">' . $this->getAdminStatusToString($v) . '</option>';
            } else {
                $options .= '<option  value="' . $v . '">' . $this->getAdminStatusToString($v) . '</option>';
            }
        }
        return $options;
    }

    /**
     * 处理7天未支付订单，自动取消
     */
    public function dealOrder() {
        $where = [
            'status' => self::STATUS_WAIT_FOR_PAY,
            'create_time' => ['lt', date('Y-m-d H:i:s', strtotime(time()-3600*24*7))],//7天自动取消
        ];
        $this->where($where)->save(['status' => self::STATUS_CANCEL]);
    }



}